package com.yunxi.controller.sat;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.DictConstant;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sat.pojo.SatLostApplyVO;
import com.yunxi.sat.service.SatLostApplyDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 客户战败申请 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-07-11
 */
@RestController
@RequestMapping("/admin/satLostApply")
@Api(tags = "sat:lostApply", description = "客户战败申请接口")
public class SatLostApplyController {

    @Autowired
    SatLostApplyDubboService satLostApplyDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult page(SatLostApplyVO satLostApplyVO, Integer pageNum, Integer pageSize,
        @CurrentUser LoginInfo loginInfo) throws Exception {
        //申请单状态
        List<Integer> applyOrderStatusList = new ArrayList<>();
        // 总经理、销售经理
        if (StringUtils.isNotEmpty(loginInfo.getRoleCode()) && StringUtils.isNotEmpty(satLostApplyVO.getIsProcess())) {
            // 销售经理
            if (DictConstant.USER_ROLE_MANAGER.equals(loginInfo.getRoleCode()) ) {
                if("0".equals(satLostApplyVO.getIsProcess())){
                    // 销售经理待审批
                    applyOrderStatusList.add(1);
                }else{
                    applyOrderStatusList.add(2);
                    applyOrderStatusList.add(3);
                    applyOrderStatusList.add(4);
                    applyOrderStatusList.add(5);
                }
            } else if (DictConstant.USER_ROLE_PARTNER_MANAGER.equals(loginInfo.getRoleCode())) {
                if("0".equals(satLostApplyVO.getIsProcess())){
                    // 总经理待审批
                    applyOrderStatusList.add(2);
                }else{
                    // 总经理同理战败
                    applyOrderStatusList.add(4);
                    // 取消
                    applyOrderStatusList.add(5);
                }
            }
            satLostApplyVO.setApplyOrderStatusList(applyOrderStatusList);
        }
        return ResponseUtil.ok(satLostApplyDubboService.findPageByVO(satLostApplyVO, pageNum, pageSize, loginInfo));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType="query", name = "applyId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String applyId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satLostApplyDubboService.getInfoById(applyId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody SatLostApplyVO satLostApplyVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satLostApplyDubboService.saveOrUpdate(satLostApplyVO,loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<String> list,@CurrentUser LoginInfo loginInfo) throws Exception {
        satLostApplyDubboService.del(list,loginInfo.getUserCode());
        return ResponseUtil.ok("success");
    }

    @PostMapping("/agree")
    @ApiOperation(value = "销售经理同意战败")
    public ApiResult agree(@RequestBody SatLostApplyVO satLostApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        String msg = satLostApplyDubboService.agree(satLostApplyVO.getApplyIdList(), loginInfo.getUserCode(),loginInfo.getRoleCode());
        return ResponseUtil.ok(msg);
    }

    @PostMapping("/dispatch")
    @ApiOperation(value = "销售经理分配")
    public ApiResult dispatch(@RequestBody SatLostApplyVO satLostApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        String msg = satLostApplyDubboService.dispatch(satLostApplyVO.getApplyIdList(), satLostApplyVO.getGetUser(), loginInfo.getUserCode(),loginInfo.getRoleCode());
        return ResponseUtil.ok(msg);
    }

    @PostMapping("/mAgree")
    @ApiOperation(value = "总经理同意战败")
    public ApiResult mAgree(@RequestBody SatLostApplyVO satLostApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        satLostApplyDubboService.mAgree(satLostApplyVO.getApplyId(),loginInfo.getUserCode(),loginInfo.getRoleCode());
        return ResponseUtil.ok("success");
    }

    @PostMapping("/mReject")
    @ApiOperation(value = "总经理驳回")
    public ApiResult mReject(@RequestBody SatLostApplyVO satLostApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        satLostApplyDubboService.mReject(satLostApplyVO.getApplyId(),loginInfo.getUserCode(),loginInfo.getRoleCode());
        return ResponseUtil.ok("success");
    }

    @GetMapping("/lostStatus")
    @ApiOperation(value = "根据客户编号查询是否战败申请中")
    @ApiImplicitParam(paramType="query", name = "customerNo", value = "客户编号", required = true, dataType = "String")
    public ApiResult lostStatus(String customerNo, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satLostApplyDubboService.lostStatus(customerNo,loginInfo.getPartnerCode()));
    }

}
